package controller.base;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import core.modules.base.entity.SysMenu;
import core.modules.base.service.SysMenuService;
import core.util.AjaxResult;
import core.util.Page;
import core.util.WebUtil;

@Controller
@RequestMapping("base/sysmenu")
public class SysMenuController {
	Logger LOGGER = LoggerFactory.getLogger(SysMenuController.class);

	@Autowired
	SysMenuService sysMenuService;

	@RequestMapping("selectIcon.htm")
	public Object IconFormSelect(String id, HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		map.put("id", id);
		return "base/sysMenu/iconFormSelect";
	}

	@RequestMapping("list.htm")
	public Object list(HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		List<SysMenu> list = sysMenuService.findList(new SysMenu());
		map.put("list", list);
		String id = WebUtil.getStr("id","0", request);
		SysMenu menu = sysMenuService.get(id);
		if (menu==null) {
			menu = sysMenuService.get("0");
		}
		map.put("menu", menu);
		return "base/sysMenu/sysMenuList2";
	}

	@RequestMapping("addChildform.htm")
	public String addChildform(SysMenu condition, HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		if (WebUtil.isNotBlank(condition.getId())) {
			condition = sysMenuService.get(condition.getId());
		}
		map.put("menu", condition);
		map.put("viewMode", WebUtil.getStr("viewMode", request));
		return "base/sysMenu/addChildsysMenuForm";
	}

	@RequestMapping("save.json")
	@ResponseBody
	public AjaxResult save(SysMenu condition, HttpServletRequest request, HttpServletResponse response) {
		AjaxResult result = null;
		try {
			sysMenuService.save(condition);
			result = new AjaxResult();
		} catch (Exception e) {
			result = new AjaxResult(false, "保存失败:" + e.getMessage());
		}
		return result;
	}

	@RequestMapping("delete.json")
	@ResponseBody
	public AjaxResult delete(String ids, HttpServletRequest request, HttpServletResponse response) {
		AjaxResult result = null;
		try {
			String[] idArray = ids.split(",");
			for (String id : idArray) {
				if (WebUtil.isNotBlank(id)) {
					sysMenuService.delete(id);
				}
			}
			result = new AjaxResult();
		} catch (Exception e) {
			result = new AjaxResult(false, "删除失败:" + e.getMessage());
		}
		return result;
	}
	
	@RequestMapping("deleteWithChildren.json")
	@ResponseBody
	public AjaxResult deleteWithChildren(String ids, HttpServletRequest request, HttpServletResponse response) {
		AjaxResult result = null;
		try {
			sysMenuService.getDao().deleteWithChildren(ids);
			result = new AjaxResult();
		} catch (Exception e) {
			result = new AjaxResult(false, "删除失败:" + e.getMessage());
		}
		return result;
	}
}
